package com.mojian.utils;

import com.mojian.common.RedisConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import javax.annotation.PostConstruct;

/**
 * @author: yuzhuo
 * @date: 2024/12/28
 * @description: 邮箱工具类
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class EmailUtil {

    @Value("${mail.smtp.email}")
    private String fromEmail;

    @Value("${mail.smtp.password}")
    private String password;

    @Value("${mail.smtp.port}")
    private int port;

    @Value("${mail.smtp.host}")
    private String host;

    private final RedisUtil redisUtil;

    private final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();

    @PostConstruct
    public void init() {
        getJavaMailSenderImpl();
    }

    public void getJavaMailSenderImpl(){
        javaMailSender.setHost(host);
        javaMailSender.setUsername(fromEmail);
        javaMailSender.setPassword(password);
        javaMailSender.setPort(port);
        javaMailSender.setDefaultEncoding("UTF-8");
        Properties p = new Properties();
        p.setProperty("mail.smtp.auth", "true");
        p.setProperty("mail.smtp.ssl.enable", "true");
        p.setProperty("mail.debug", "true");
        javaMailSender.setJavaMailProperties(p);
    }

    /**
     * 发送验证码
     * @param email
     * @throws MessagingException
     */
    public void sendCode(String email) throws MessagingException {
        this.getJavaMailSenderImpl();

        int code = (int) ((Math.random() * 9 + 1) * 100000);
        String content = "<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>邮箱验证码</title>\n" +
                "</head>\n" +
                "<body style=\"margin: 0; padding: 0; font-family: Arial, sans-serif;\">\n" +
                "    <div style=\"max-width: 600px; margin: 0 auto; padding: 20px;\">\n" +
                "        <div style=\"background-color: #f8f9fa; padding: 20px; border-radius: 5px;\">\n" +
                "            <div style=\"text-align: center; margin-bottom: 20px;\">\n" +
                // "                <img src=\"https://foruda.gitee.com/avatar/1739413372327645883/13781_d029020a68_1739413372.png\" alt=\"Logo\" style=\"width: 50px; height: 50px;\">\n" +
                "                <h1 style=\"color: #333; margin: 10px 0;\">暖心社</h1>\n" +
                "            </div>\n" +
                "            <div style=\"background-color: #fff; padding: 20px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);\">\n" +
                "                <h2 style=\"color: #333; margin-top: 0;\">邮箱验证码</h2>\n" +
                "                <p style=\"color: #666; line-height: 1.6;\">您好，感谢您使用暖心社。以下是您的验证码：</p>\n" +
                "                <div style=\"background-color: #f8f9fa; padding: 15px; text-align: center; margin: 20px 0; border-radius: 5px;\">\n" +
                "                    <span style=\"font-size: 24px; font-weight: bold; color: #007bff;\">" + code + "</span>\n" +
                "                </div>\n" +
                "                <p style=\"color: #666; line-height: 1.6;\">验证码有效期为5分钟，请尽快使用。</p>\n" +
                "                <p style=\"color: #999; font-size: 12px; margin-top: 20px;\">此邮件由系统自动发送，请勿回复。</p>\n" +
                "            </div>\n" +
                "            <div style=\"text-align: center; margin-top: 20px; color: #666; font-size: 12px;\">\n" +
                "                <p>© 2025 暖心社. All rights reserved.</p>\n" +
                // "                <p><a href=\"https://www.shiyit.com\" style=\"color: #007bff; text-decoration: none;\">www.shiyit.com</a></p>\n" +
                "            </div>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>";

        // 创建邮件消息
        this.send(email, content);
        log.info("邮箱验证码发送成功,邮箱:{},验证码:{}", email, code);

        redisUtil.set(RedisConstants.CAPTCHA_CODE_KEY + email, code + "");
        
        redisUtil.expire(RedisConstants.CAPTCHA_CODE_KEY + email, RedisConstants.FIVE_MINUTES_EXPIRE, TimeUnit.SECONDS);
    }

    private void send(String email, String template) throws MessagingException {
        //创建一个MINE消息
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mineHelper = new MimeMessageHelper(mimeMessage, true);
        // 设置邮件主题
        mineHelper.setSubject("暖心社- 邮箱验证码");
        // 设置邮件发送者
        mineHelper.setFrom(Objects.requireNonNull(javaMailSender.getUsername()));
        // 设置邮件接收者
        mineHelper.setTo(email);
        // 设置邮件发送日期
        mineHelper.setSentDate(DateUtil.getNowDate());
        // 设置邮件的正文
        mineHelper.setText(template, true);
        // 发送邮件
        javaMailSender.send(mimeMessage);
    }


}
